package com.soft.mainsite.component.beetl.tag.district;

import cn.hutool.core.convert.Convert;
import com.soft.mainsite.common.exception.CmsException;
import com.soft.mainsite.module.city.entity.City;
import com.soft.mainsite.module.city.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DistrictListTag此类用于默认地区显示
 *
 * @author: hujm
 * @date: 2019年08月02日 10:54
 * @remark:
 */
@Slf4j
@Service
@Scope("prototype")
public class DistrictListTag extends GeneralVarTagBinding {

    @Autowired
    private CityService cityService;

    /**
     * 根据站点ID查询站点城市下的区县列表信息
     */
    @Override
    public void render() {
        Integer cityCode = Convert.toInt(this.getAttributeValue("cityCode"));
        try {
            this.wrapRender(cityCode);
        } catch (Exception e) {
            throw new CmsException("DistrictListTag wrapRender异常 == " + e.getMessage());

        }
    }

    private void wrapRender(Integer cityCode) {
        List<City> districtList = cityService.getCityDistrictByCityParentCode(cityCode);
        for (City city : districtList) {
            Map<String, Object> map = new HashMap<>(16);
            map.put("cityName", city.getCityName());
            map.put("cityCode", city.getCityCode());
            this.binds(map);
            this.doBodyRender();
        }

    }
}
